package com.nxmu.hire.inceptor;

import com.nxmu.hire.common.constant.UserConstant;
import com.nxmu.hire.common.util.CookieUtil;
import com.nxmu.hire.model.User;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

/**
 * 登录拦截器
 */
public class LoginInterceptor extends HandlerInterceptorAdapter {

    private final static String LOGIN_URI = "/showLogin";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        String requestURI = request.getRequestURI();

//        if (requestURI.contains(LOGIN_URI) || requestURI.contains(LOGIN_PAGE) || requestURI.contains(SWAGGER)) return true;

        if (!StringUtils.isEmpty(requestURI)) {
//            User user = (User) request.getSession().getAttribute(UserConstant.SESSION_USER);
            User user = CookieUtil.cookieToObject(User.class, request.getCookies(), UserConstant.SESSION_USER);
            if (!Objects.isNull(user)) {
                return true;
            }
        }
        request.getRequestDispatcher(LOGIN_URI).forward(request,response);
        return false;
    }

}
